<?php

class DBLink {

    private static /* DBLink */ $instance;
    private $pdo;

    private function __construct() {
        include "config.php";
        $this->pdo = new PDO("mysql:host=".$host.";dbname=".$db_name, $db_user, $db_pass);
    }

    public static function getInstance() {
        if (DBLink::$instance == null) {
            DBLink::$instance = new DBLink();
        }
        return DBLink::$instance;
    }

    public function query($query) {
        return $this->pdo->query($query);
    }

    private function translate($rules, $attribute, $value) {
        
    }

    public function store(DBItem $dbitem) {
        if ($dbitem->new_flag == true) {
            $rules = $dbitem->mapping->getRules();
            $table = $dbitem->mapping->getTable();
            $data = $dbitem->getData();
            $INSERT= "INSERT INTO ".$table."(";
            $VALUES = " VALUES (";
            foreach (array_keys($data) as $key) {
                if ((isset($dbitem->dirtyflags[$key])) && ($dbitem->dirtyflags[$key] == true)) {
                    if ($rules[$key]->is_writable == true) {
                        $INSERT = $INSERT."".$rules[$key]->column.",";
                        $VALUES = $VALUES."'".$data[$key]."',";
                    }
                }
            }
            $INSERT=substr($INSERT, 0, strlen($INSERT)-1).")";
            $VALUES=substr($VALUES, 0, strlen($VALUES)-1).")";
            
            $q = $this->pdo->prepare($INSERT."".$VALUES)->execute();
            $lastInsertId = $this->pdo->lastInsertId();
            $dbitem->__set($dbitem->mapping->getPrimaryKeyRule()->attributeName, $lastInsertId);


        } else if ($dbitem->dirty_flag == true) {
            $rules = $dbitem->mapping->getRules();
            $table = $dbitem->mapping->getTable();
            $data = $dbitem->getData();
            $SET = " SET ";
            $UPDATE = "UPDATE " . $table;
            foreach (array_keys($data) as $key) {
                if ((isset($dbitem->dirtyflags[$key])) && ($dbitem->dirtyflags[$key] == true)) {
                    if ($rules[$key]->is_writable == true) {
                        $SET = $SET . " " . $rules[$key]->column . "='".$data[$key]."',";
                    }
                }
            }
            $SET=substr($SET, 0, strlen($SET)-1);
            $WHERE = " WHERE ".$dbitem->mapping->getPrimaryKeyRule()->column."='".$dbitem->getKey()."'";
            $query = $this->pdo->prepare($UPDATE . "" . $SET . "" . $WHERE);
            print_r($query->execute());
            $dbitem->updateKey();
        }
    }

    public function delete(DBItem $dbitem) {
        $table = $dbitem->mapping->table;
        $idfield = $dbitem->mapping->primaryKeyRule->column;
        $idattribute = $dbitem->mapping->primaryKeyRule->attributeName;
        $query = $this->pdo->prepare("DELETE FROM ".$table." where ".$idfield." = '".$dbitem->__get($idattribute)."'");
        $res = $query->execute();
        return $res;
    }

    public function begin() {
        return $this->pdo->beginTransaction();
    }

    public function commit() {
        return $this->pdo->commit();
    }

    public function rollBack() {
        return $this->pdo->rollBack();
    }

}
?>
